package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.base.PhaseExt;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseext.PhaseExtEditReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseext.PhaseExtGetReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.phaseext.PhaseExtGetResVo;
import com.zmj.sy.mom.srv.aps.mapper.PhaseExtMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service
public class PhaseExtService {

    private final PhaseExtMapper phaseExtMapper;

    public PhaseExtGetResVo get(PhaseExtGetReqVo reqVo) {
        PhaseExt one = phaseExtMapper.lambdaQuery().eq(PhaseExt::getPhaseCode, reqVo.getPhaseCode()).one();
        PhaseExtGetResVo resVo = new PhaseExtGetResVo();
        if(one == null){
            resVo.setPhaseCode(reqVo.getPhaseCode());
            return resVo;
        }
        BeanUtils.copyProperties(one, resVo);
        return resVo;
    }

    @Transactional
    public void edit(PhaseExtEditReqVo reqVo) {
        PhaseExt one = phaseExtMapper.lambdaQuery().eq(PhaseExt::getPhaseCode, reqVo.getPhaseCode()).one();
        if(one == null){
            one = new PhaseExt();
            one.setPhaseCode(reqVo.getPhaseCode());
            one.setFlowTime(reqVo.getFlowTime());
            phaseExtMapper.insert(one);
            return ;
        }

        one.setFlowTime(reqVo.getFlowTime());
        phaseExtMapper.updateById(one);
    }
}
